package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;

public class BandaoNewAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		//http://news.bandao.cn/news_html/201211/20121122/news_20121122_2025221.shtml?i|180582:1
		String[] regex={
				"http://.*.bandao.cn/news_html/[0-9]{6}/[0-9]{8}/news_[0-9]{8}_[0-9]*[.]shtml.*?",
				"http://.*.bandao.cn/news_uhtml/[0-9]*/unews_[0-9]*_[0-9]*.shtml"
				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet=true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url=urlMeta.getUrl();
		/**
		 * 判断是否为详细页，不是详细页面直接返回空
		 */
		if (!isDetailPage(url)) {
		}
		String htmltxt=urlMeta.getHtml();
		String title="";
		String content="";
		Long date=null;
		Integer commentNum=0;
		Integer clickNum=0;
		NewsMeta news=new NewsMeta();
		Document doc=Jsoup.parse(htmltxt);
		Elements select=doc.select("div.area570");
		if(select.size() > 0){
			title=select.select("h1").text();
			/**
			 * 解析时间
			 */
			Elements select_date=select.select("p.biaotiDate");
			if(select_date.size() > 0){
				date=DateUtils.matchDate(select_date.toString().replace("&nbsp;", " "));
			}
			/**
			 * 解析内容
			 */
			select=select.select("div#textContent");
			if(select.size() > 0){
				content=HtmlCleaner.getContentHtml(url,select);
			}
		}
		if(StringUtils.isBlank(title)){
			title=doc.select("h1").text();
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div#textContent"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select(".TextContent"));
		}
		if(date == null){
			date=DateUtils.matchDate(doc.select("span.dateK").text());
		}
		if(date == null){
			date=DateUtils.matchDate(doc.select(".titleM").text());
		}
		news.setTitle(title);
		news.setContent(content);
		news.setDate(date);
		news.setUrl(url);
		news.setType(0);
		news.setCommentNum(commentNum);
		news.setClickNum(clickNum);
		String author= JSoupUtils.matchAuthor(doc, "来源:");
		news.setAuthor(author);
		return news;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		BandaoNewAnalyse b=new BandaoNewAnalyse();
//		String url="http://news.bandao.cn/news_html/201211/20121122/news_20121122_2025221.shtml?i|180582:1";
		String url="http://news.bandao.cn/news_uhtml/201611/unews_20161108_34657.shtml";
		System.out.println(b.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=b.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
